import { staticRoutes } from "@/router/routes/staticRoutes";
import type { CascaderOption } from 'element-plus';
import { AuthRoute } from "~/src/typings/router";

const funcBtnOptions: CascaderOption[] = [
  {
    value: 'search',
    label: '搜索'
  },
  {
    value: 'create',
    label: '新建'
  },
  {
    value: 'update',
    label: '更新'
  },
  {
    value: 'delete',
    label: '删除'
  }
];

/**
 * 将路由转化为级联组件options格式
 * @param router 全局路由
 */
export function routerToCascader(router: AuthRoute.Route[] = staticRoutes): CascaderOption[] {
  if (Array.isArray(router)) {
    return router?.reduce((pre: CascaderOption[], cur) => {
      const { name, meta, children } = cur;
      const map: CascaderOption = {};
      map.label = meta?.title;
      map.value = name;
      if (children) {
        map.children = routerToCascader(children);
      }else {
        map.children = funcBtnOptions;
      }
      pre.push(map);
      return pre;
    }, []);
  }
  return [];
}

